using System;
using System.Data;
using Headspring.Training.Domain.Model;
using NHibernate.Type;

namespace Headspring.Training.DataAccess.Impl
{
	public class WorkOrderStatusType : ValueTypeType
	{
		public WorkOrderStatusType() : base(new NHibernate.SqlTypes.StringFixedLengthSqlType(3))
		{
		}

		public override object Get(IDataReader rs, int index)
		{
			string value = (string)rs[index];
			return WorkOrderStatus.FromCode(value);
		}

		public override object Get(IDataReader rs, string name)
		{
			int ordinal = rs.GetOrdinal(name);
			return Get(rs, ordinal);
		}

		public override Type ReturnedClass
		{
			get { return typeof(WorkOrderStatus); }
		}

		public override string ObjectToSQLString(object value)
		{
			return value.ToString();
		}

		public override object FromStringValue(string xml)
		{
			return byte.Parse(xml);
		}

		public override string Name
		{
			get { return "WorkOrderStatus"; }
		}

		public override void Set(IDbCommand cmd, object value, int index)
		{
			IDataParameter parameter = (IDataParameter)cmd.Parameters[index];

			WorkOrderStatus val = (WorkOrderStatus)value;

			parameter.Value = val.Code;
		}

		public override bool Equals(object x, object y)
		{
			//get boxed values.
			WorkOrderStatus xTyped = x as WorkOrderStatus;
			if(xTyped == null) return false;

			return xTyped.Equals(y);
		}
	}
}